package tpalgo3.vista;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import tpalgo3.juego.Juego;
import tpalgo3.util.CoordenadasUtil;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class Tablero extends JPanel {
	private static final long serialVersionUID = -2389995068717480187L;

	private static int NUMERO_DE_FILAS = 20;
	private static int NUMERO_DE_COLUMNAS = 20;
	public static TableroSquare square;
	
	private Juego juego;

	private void createSquares() {
		CellConstraints cc = new CellConstraints(); 
		for (int i = 0; i < NUMERO_DE_FILAS; i++) {
			for (int j = 0; j < NUMERO_DE_COLUMNAS; j++) {
				square = new TableroSquare();
				pintarCamino(j, i, square);
				square.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
				square.setPreferredSize(new Dimension(35, 35));
				add(square, cc.xy(j + 1, i + 1));
			}
		}
	}

	private void pintarCamino(final int x, final int y, TableroSquare square) {
		if (CoordenadasUtil.perteneceAlCamino(new Point(x, y), juego.getCamino())) {
			square.setBackground(new Color(255, 255, 255));
			square.setEquis(x);
			square.setIgriega(y);
			square.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					if (e.getClickCount() == 2) {
						TableroSquare tSquare = (TableroSquare)e.getSource();
						new SeleccionObstaculo(juego, Tablero.this, tSquare.getEquis(), tSquare.getIgriega());
					}
				}
			});
		}
		else {
			square.setBackground(new Color(0, 0, 0));
			square.setEquis(x);
			square.setIgriega(y);
			square.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					if (e.getClickCount() == 2) {
						TableroSquare tSquare = (TableroSquare)e.getSource();
						new SeleccionTorre(juego, Tablero.this, tSquare.getEquis(), tSquare.getIgriega());
					}
				}
			});
		}
	}
	
	public Tablero(Juego juego) {
		super(new FormLayout(
				new ColumnSpec[]{
					new ColumnSpec("35px"),
					new ColumnSpec("35px"),
					new ColumnSpec("35px"),
					new ColumnSpec("35px"),
					new ColumnSpec("35px"),
					new ColumnSpec("35px"),
					new ColumnSpec("35px"),
					new ColumnSpec("35px"),
					new ColumnSpec("35px"),
					new ColumnSpec("35px"),
					new ColumnSpec("35px"),
					new ColumnSpec("35px"),
					new ColumnSpec("35px"),
					new ColumnSpec("35px"),
					new ColumnSpec("35px"),
					new ColumnSpec("35px"),
					new ColumnSpec("35px"),
					new ColumnSpec("35px"),
					new ColumnSpec("35px"),
					new ColumnSpec("35px")
				},
				new RowSpec[]{
					new RowSpec("35px"),
					new RowSpec("35px"),
					new RowSpec("35px"),
					new RowSpec("35px"),
					new RowSpec("35px"),
					new RowSpec("35px"),
					new RowSpec("35px"),
					new RowSpec("35px"),
					new RowSpec("35px"),
					new RowSpec("35px"),
					new RowSpec("35px"),
					new RowSpec("35px"),
					new RowSpec("35px"),
					new RowSpec("35px"),
					new RowSpec("35px"),
					new RowSpec("35px"),
					new RowSpec("35px"),
					new RowSpec("35px"),
					new RowSpec("35px"),
					new RowSpec("35px")
				}
			));
		this.juego = juego;
		initComponents();
	}

	private void initComponents() {
		createSquares();
	}
}